/**
 * Collection lists
 * -- question
 * -- question_replystatistics
 * -- question_replypraises * --
 * -- question_replythanks
 * -- question_reports
 * -- question_replycounts
 */

var Schema = mongoose.Schema;


//======================question =============================
var questionMongoSchema = new Schema({
    displayOrder: Number,                             //问题或者投票的显示顺序
    categoryOrder: Number,                            //分类里问题显示顺序
    allOrder: Number,                                 //全部置顶
    community: {type: Schema.Types.ObjectId, ref: 'community'},             //所属小区
    city: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},         //城市
    aroundCommunity: [                                                      //周边小区
        {type: Schema.Types.ObjectId, ref: 'community', index: true}
    ],
    category: {
        type: String,
        enum: ['question', 'share', 'activity', 'secondhand', 'free', 'lostAndFound', 'advertisement']
    },   //类型: 求助、分享、活动、二手、免费赠送、失物招领、广告

    //问题和投票的公共字段==========================
    status: {type: String, enum: ['未解决', '已解决', '进行中', '已关闭']},   //问题或者投票状态
    tag: [
        {type: Schema.Types.ObjectId, ref: 'masterData_Dictionaries'}        //关联到兴趣爱好的schema ID
    ],                                                                       //问题或者投票标签
    browserCount: {type: Number, default: 0},                               //浏览数量
    praiseCount: {type: Number, default: 0},                                    //点赞数量
    recommend: {type: Boolean, default: false},
    title: String,   //专题标题
    desc: String,    //问题或者投票的描述
    images: [       //问题或者投票的图片
        {
            imageURL: String,
            isValid: {type: Boolean, default: true}
        }],

    //问题的回答字段
    replies: [{
        displayOrder: Number,  //回答的显示顺序
        replyStatus: {type: String, enum: ['已采纳', '未采纳'], default: '未采纳'},
        replyDesc: String,
        images: [
            {
                imageURL: String,
                isValid: {type: Boolean, default: true}
            }],
        replyType: String,
        isValid: {type: Boolean, default: true},
        replyTo: {type: Schema.Types.ObjectId, ref: 'user'},
        repliedBy: {type: Schema.Types.ObjectId, ref: 'user'},
        dateReplied: {type: Date, default: Date.now}
    }],

    level: {type: Number, default: 0},                      //0,普通帖子 1,精华贴
    acrossCity: {type: Number, default: 0},                 //0,普通帖子 1,同城
    topicId: {type: Schema.Types.ObjectId, ref: 'topic'},    //话题id
    isShow:Number,                                           //设置屏蔽 赋值-1 ，取消屏蔽 ，删除此字段
    isValid: {type: Boolean, default: true},
    delDesc:String,                                          //删除原因描述
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'},
    dateCreated: {type: Date, default: Date.now},
    fromFlag: String,
    dateLastModified: {type: Date, default: Date.now},
    topBeginDate : {type: Date},            //置顶开始日期
    topEndDate : {type: Date},              //置顶结束日期
    /*  宝宝树66小区二次改造    add   chenf  20170731   start   */
    topFlg: {type: Number, default: 0},     // 置顶flg  （0：没有置顶（默认值），1：全国，2：本小区）
    /*  宝宝树66小区二次改造    add   chenf  20170731   end   */
    remark: String
});
mongoose.model('question', questionMongoSchema, 'question');


//====================== question_replystatistics ===========================

var questionReplyStatisticsMongoSchema = new Schema({
    community: {type: Schema.Types.ObjectId, ref: 'community'},        //所属小区
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    tag: {type: Schema.Types.ObjectId, ref: 'masterData_Dictionaries'},  //问题或者投票标签
    replyCount: {type: Number, default: 0},
    adoptedCount: {type: Number, default: 0}
});
mongoose.model('question_replystatistics', questionReplyStatisticsMongoSchema, 'question_replystatistics');


// ===================question_replypraises=======================

var replyPraiseStatisticsMongoSchema = new Schema({
    reply: {type: Schema.Types.ObjectId},
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    replyBy: {type: Schema.Types.ObjectId, ref: 'user'},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('question_replypraises', replyPraiseStatisticsMongoSchema, 'question_replypraises');


//======================question_replythanks================================

var replyThankStatisticsMongoSchema = new Schema({
    reply: {type: Schema.Types.ObjectId},
    replyBy: {type: Schema.Types.ObjectId, ref: 'user'},
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('question_replythanks', replyThankStatisticsMongoSchema, 'question_replythanks');


//=====================question_reports==========================
var questionReportMongoSchema = new Schema({
    question: {type: Schema.Types.ObjectId, ref: 'question'},
    questionCommunity: {type: Schema.Types.ObjectId, ref: 'community'},           //question所属小区
    questionCreateBy : {type: Schema.Types.ObjectId, ref: 'user'},
    reportStatus : {type: Number, default: 0},                                  //-1:已忽略 0：未处理 1：已删除
    activity: {type: Schema.Types.ObjectId, ref: 'activity_neighbour'},
    reportUser: {type: Schema.Types.ObjectId, ref: 'user'},
    status: {type: Boolean, default: false},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('question_reports', questionReportMongoSchema, 'question_reports');


//=========================question_replycounts===============================
var questionAndReplyCount = new Schema({
    questionCount: {type: Number, default: 0},
    replyCount: {type: Number, default: 0},
    tag: {type: Schema.Types.ObjectId, ref: 'masterData_Dictionaries'},
    community: {type: Schema.Types.ObjectId, ref: 'community'}
});
mongoose.model('question_replycounts', questionAndReplyCount, 'question_replycounts');

var questionAdoptedCount = new Schema({
    community: {type: Schema.Types.ObjectId, ref: 'community'},        //所属小区
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    adoptedCount: {type: Number, default: 0},
    dateCreated: {type: Date, default: Date.now},
    dateLastModified: {type: Date, default: Date.now}
});
mongoose.model('question_adoptedcounts', questionAdoptedCount, 'question_adoptedcounts');

var replyAtSchema = new Schema({
    replyId: String,
    uList: [
        {type: Schema.Types.ObjectId, ref: 'user'}
    ],
    ugcId: String,
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'},
    category: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('question_replyAt', replyAtSchema, 'question_replyAt');

var replyAtServiceSchema = new Schema({
    category: String,
    ugcId: String,
    content: String,
    replyId: String,
    atUser: {type: Schema.Types.ObjectId, ref: 'user'},
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    status: {type: Boolean, default: true},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('question_replyAtService', replyAtServiceSchema, 'question_replyAtService');

var questionPraiseSchema = new Schema({
    question: {type: Schema.Types.ObjectId, ref: 'question'},
    activity: {type: Schema.Types.ObjectId, ref: 'activity_neighbour'},
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('question_praise', questionPraiseSchema, 'question_praise');

var questionstoreMongoSchema = new Schema({

    storetype :{
        type: String,
        enum: ['op', 'init']
    },   // 运维: 初始化,
    desc: String,    //问题或者投票的描述
    category: {
        type: String,
        enum: ['question', 'share', 'activity', 'secondhand', 'free', 'lostAndFound', 'advertisement']
    },   //类型: 求助、分享、活动、二手、免费赠送、失物招领、广告
    images: [       //问题或者投票的图片
        {
            imageURL: String,
            isValid: {type: Boolean, default: true}
        }],
    tag: [
        {type: Schema.Types.ObjectId, ref: 'masterData_Dictionaries'}        //关联到兴趣爱好的schema ID
    ],
    level: {type: Number, default: 0},                      //0,普通帖子 1,精华贴

    isValid: {type: Boolean, default: true},
    delDesc:String,                                         //删除原因描述
    status: {type: Number, default: 0},                    //0：未发布 1：已发布
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'},
    dateCreated: {type: Date, default: Date.now},
    dateLastModified: {type: Date, default: Date.now}
});
mongoose.model('question_store', questionstoreMongoSchema, 'question_store');

var topicMongoSchema = new Schema({
    title: String,   //话题标题
    subTitle: String,    //话题副标题
    desc: String,         //话题描述
    browserCount: {type: Number, default: 0},                             //浏览数量
    images: [       //问题或者投票的图片
        {
            imageURL: String,
            isValid: {type: Boolean, default: true}
        }],
    shareKey : String,
    shareDesc: String,         // 分享描述
    shareImage : String,
    isValid: {type: Boolean, default: true},
    dateCreated: {type: Date, default: Date.now}
});

mongoose.model('topic', topicMongoSchema, 'topic');


var replyIndexSchema = new Schema({
    contentId: String,
    replyId: String,
    replyStatus: {type: String, enum: ['已采纳', '未采纳'], default: '未采纳'},
    repliedBy: {type: Schema.Types.ObjectId, ref: 'user'},
    dateReplied: {type: Date, default: Date.now}
});
replyIndexSchema.index({dateReplied: -1});
mongoose.model('reply_index', replyIndexSchema, 'reply_index');